﻿@using TDF.Core.Json
@model Faurecia.WM.Web.Models.ProductionRuleConfigModel
<style>
    #table-container-bbb .fixed-table-pagination {
        display: none;
    }
    .table-container {
        width: auto;
    }
</style>
@if ((bool)ViewBag.ShowBBBConfig)
{
    <div class="portlet light portlet-fit portlet-datatable bordered">
        <div class="portlet-title">
            <div class="caption">
                <i class="icon-settings"></i>
                <span class="caption-subject sbold uppercase">生产计划调配(BBB)</span>
            </div>
        </div>
        <div class="portlet-body">
            <div class="table-container" id="table-container-bbb">
                <table id="table-bbb" data-toggle="table"
                       data-height="100"
                       data-only-info-pagination="true"
                       data-width="100%"
                       data-pagination="false"></table>
            </div>
        </div>
    </div>
}
<div class="portlet light portlet-fit portlet-datatable bordered">
    <div class="portlet-title">
        <div class="caption">
            <i class="icon-settings"></i>
            <span class="caption-subject sbold uppercase">生成计划</span>
        </div>
    </div>
    <div class="portlet-body">
        <div class="table-container" id="table-container-rule">
            <table id="table-rule" data-toggle="table"
                   data-height="800"
                   data-only-info-pagination="true"
                   data-pagination="true"></table>
        </div>
    </div>
</div>
<script type="text/javascript">
    var BBBConfig = JSON.parse('@Html.Raw(Model.BBBConfig.ToJson())');
    var ProdctionRule = JSON.parse('@Html.Raw(Model.ProdctionRule.ToJson())');
    var columnIndex = 0;
    for (var i = 0; i < BBBConfig.columns.length; i++) {
        //obj.columns[i].formatter = function (value, row, index) {
        //    var str = value ? "是" : "否";
        //    return "<a href=\"#\" name=\"EnabledBBB\" data-type=\"select\" data-pk=\"" + index + "\"  data-title=\"是否启用BBB\">" + str + "</a>";
        //};
        BBBConfig.columns[i].formatter = function (value, row, index) {
            var machineId = '';
            var i = 0;
            for (var item in row) {
                if (row.hasOwnProperty(item)) {
                    if (i++ === columnIndex) {
                        machineId = item;
                        break;
                    }
                }
            }
            columnIndex++;
            return '<select class="sel-bbb" data-machine-id="' + machineId + '" style="width:100%">' +
                '<option value="true" ' + (value ? 'selected="selected"' : '') + '>' + '是' + '</option>' +
                 '<option value="false" ' + (value ? '' : 'selected="selected"') + '>' + '否' + '</option>' +
                '</select>';
        };
    }

    BBBConfig.columns.unshift({
        title: '&nbsp;&nbsp;',
        width: '10%'
    });
    BBBConfig.columns.push({
        title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
            '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
            '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
        width: '10%'
    });
    $("#table-bbb").bootstrapTable(BBBConfig);
    $("#table-bbb .sel-bbb").change(function() {
        var enabled = $(this).val();
        var machineId = $(this).attr("data-machine-id");
        $.ajax({
            url: '@Url.Action("EnableBBB")',
            data: { enabled: enabled, machineId: machineId, workTimeId: workTimeId },
            type: 'post',
            success: function (result) {
                if (!result.Success) {
                    showToast(3, '提示', result.Message);
                }
            }
        });
    });
   var rowIndex2 = -1;
    for (var i = 0; i < ProdctionRule.columns.length; i++) {
        //obj.columns[i].formatter = function (value, row, index) {
        //    var str = value ? "是" : "否";
        //    return "<a href=\"#\" name=\"EnabledBBB\" data-type=\"select\" data-pk=\"" + index + "\"  data-title=\"是否启用BBB\">" + str + "</a>";
        //};
        ProdctionRule.columns[i].formatter = function (value, row, index) {
            var flag = false;
            for (var item in row) {
                if (row.hasOwnProperty(item)) {
                    if (rowIndex2 !== index) {
                        flag = true;
                        rowIndex2 = index;
                        break;
                    }
                }
            }
            if (flag) {
                return '<span data-mould="' + row.PropertyInfos + '">' + value + '</span>';
            }
            return value;
        };
        if (i !== 0 && i !== ProdctionRule.columns.length-1) {
            ProdctionRule.columns[i].editable = {
                type: 'text',
                title: '分配比例',
                validate: function (value) { //字段验证
                    if (!$.trim(value)) {
                        return '不能为空';
                    }
                    if (!/^\+?[0-9][0-9]*$/.test(value)) {
                        return '只能输入正整数';
                    }
                },
                url: '@Url.Action("SaveRule")',
                params: function (params) {
                    params.WorkTimeId = workTimeId;
                    params.Mould = $(this).parent().parent().find("td:eq(0) span").attr("data-mould");
                    params.MachineId = params.name;
                    debugger;
                    return params;
                }

            }
        }
        if (i === ProdctionRule.columns.length - 1) {
            ProdctionRule.columns[i].editable = {
                type: 'text',
                title: '最大堆叠数',
                validate: function (value) { //字段验证
                    if (!$.trim(value)) {
                        return '不能为空';
                    }
                    if (!/^\+?[0-9][0-9]*$/.test(value)) {
                        return '只能输入正整数';
                    }
                    return;
                },
                url: '@Url.Action("SetReleasedUpperLimit")',
                params: function (params) {
                    params.WorkTimeId = workTimeId;
                    params.Mould = $(this).parent().parent().find("td:eq(0) span").attr("data-mould");
                    return params;
                }
            }
        }
    }

    $("#table-rule").bootstrapTable(ProdctionRule);
    window.onresize = function () {
        $("#table-bbb").bootstrapTable('resetWidth');
        $("#table-rule").bootstrapTable('resetWidth');
    }

</script>